﻿using System;
using System.IO;

namespace MyMathLib
{
    public class Data
    {   

        /// <summary>
        /// 求b值
        /// </summary>
        /// <param name="years"></param>
        /// <param name="gdps"></param>
        /// <returns></returns>
        public static double GetNumber_b(List<int> years,List<double> gdps)
        {
            double temp1 = 0;
            double temp2 = 0;
            double temp3 = 0;
            for(int i = 0; i < years.Count; i++)
            {
                temp1 += (years[i] - years.Average()) * (gdps[i] - gdps.Average());
                temp2 += (years[i] - years.Average()) * (years[i] - years.Average());
                temp3 += years[i];
            }
            return temp1 / temp2;
        }
        /// <summary>
        /// 求a值
        /// </summary>
        /// <param name="years"></param>
        /// <param name="gdps"></param>
        /// <returns></returns>
        public static double GetNumber_a(List<int> years, List<double> gdps)
        {
            return gdps.Average()-GetNumber_b(years, gdps)*years.Average();
        }

        /// <summary>
        /// 定义重载函数用于写入文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="list"></param>
        public  void ListReadTxt(string path, List<int> Year)
        {
            if (File.Exists(path))
            {
                List<int> list = Year;
                StreamReader sr = new StreamReader(path);
                string[] ss;
                string line = sr.ReadLine();
                ss = line.Split(' ');
                foreach (string v in ss)
                    list.Add(int.Parse(v));
                sr.Close();
            }
        }
        public void ListReadTxt(string path, List<double> Gdps)
        {
            if (File.Exists(path))
            {
                List<double> list = Gdps;
                StreamReader sr = new StreamReader(path);
                string[] ss;
                string line = sr.ReadLine();
                ss = line.Split(' ');
                for (int i = 0; i < ss.Length; i++)
                    list.Add(double.Parse(ss[i]));
                sr.Close();
            }
        }

    }

    public class MyFunction
    { 
        public static void ListReadTxt(string path, List<int> ints)
        {
            if (File.Exists(path))
            {
                
                StreamReader sr = new StreamReader(path);
                string[] ss;
                string line = sr.ReadLine();
                ss = line.Split(' ');
                for (int i = 0; i < ss.Length; i++)
                    ints.Add(int.Parse(ss[i]));
                sr.Close();
            }
        }

        public static void ListReadTxt(string path, List<double> doubles)
        {
            StreamReader sr = new StreamReader(path);
            string[] ss;
            string line = sr.ReadLine();
            ss = line.Split(' ');
            for(int i = 0; i < ss.Length; i++)
                doubles.Add(double.Parse(ss[i]));
            sr.Close();
        }


    }
}